import SceneDialog from './SceneDialog/SceneDialog.vue';
import SafeDialog from './SafeDialog/SafeDialog.vue';
import BaseDialog from './BaseDialog/BaseDialog.vue';

export default {
    components: {
        SceneDialog,
        SafeDialog,
        BaseDialog
    },
    name: 'ConstrctionTask',
    data() {
        return {
            title: '',
            //
            loading: true,
            // 当前页
            currentPage: 1,
            // 每页显示多少条
            pageSize: 10,
            // 总页数
            total: 1,
            // 展开收起搜索条件
            expandConditionFlag: false,
            // 施工现场
            sceneFlag: false,
            // 安全施工
            safeFlag: false,
            dialogFlag: false,
            // 工单编号
            workOrderNo: '',
            // 任务名称 用于查询字段
            taskName: '',
            // 任务名称 用于显示
            taskNameTitle: '',
            // 任务名称枚举，用于显示弹框
            taskNameFlag: 0,
            //
            taskId: '',
            // 状态
            workOrderStatus: '',
            orderStatusList: [
                {
                    value: '1',
                    label: '草稿'
                },
                {
                    value: '2',
                    label: '已提交'
                },
                {
                    value: '3',
                    label: '已驳回'
                },
                {
                    value: '4',
                    label: '已通过'
                }
            ],
            // 表格数据
            tableData: [],
            multipleSelection: [],
            workOrderNumber: '', //工单编号
            projectName: '',
            orderTypeList: [] //装修类型
        };
    },
    created() {
        // 获取表格数据  current = 1, size = 10
        this.orderTableData(1, 10);
        this.$httpExt()
            .get('/api/common/getSelections', {
                pkeyPath: 'BisArguSetting.construction.serviceItems'
            })
            .then(
                response => {
                    this.orderTypeList = response.data;
                    console.log('字典返回值', response.data);
                },
                response => {
                    console.log('异常', response);
                }
            );
    },
    methods: {
        // 每页显示多少条, 重置current从1开始
        handleSizeChange(val) {
            this.pageSize = val;
            this.currentPage = 1;
            this.orderTableData(this.currentPage, val);
        },
        // 当前页数
        handleCurrentChange(val) {
            this.orderTableData(val, 10);
        },
        status(status) {
            let result = this.orderStatusList.filter(item => item.value === status)[0];
            return result.label;
        },
        task(val) {
            let valNum = parseInt(val);
            if (valNum === 1) {
                return '材料进场验收';
            } else if (valNum === 2) {
                return '隐蔽工程验收';
            } else if (valNum === 3) {
                return '施工现场巡逻';
            } else if (valNum === 4) {
                return '安全施工巡查';
            } else {
                return '';
            }
        },
        // 处理后端数据
        formatResult(list = []) {
            // 处理后端数据格式
            const resultFormat = list.map(item => {
                return {
                    // 工单状态
                    taskStatus: item.taskStatus,
                    // 工单编号
                    workOrderNumber: item.workOrderNumber,
                    // 工程名称
                    projectName: item.projectName,
                    // 任务名称
                    taskName: item.taskName,
                    // 开工日期
                    startTime: item.startTime,
                    // 竣工日期
                    endTime: item.endTime,
                    // 装修类型
                    orderType: item.orderType,
                    // 装修面积
                    renovationTotalArea: item.renovationTotalArea,
                    // 创建人
                    createEmp: item.createEmp,
                    //
                    createTime: item.createTime,
                    //
                    id: item.taskId
                };
            });
            return resultFormat;
        },
        // 获取表格数据
        async orderTableData(current, size) {
            this.loading = true;
            this.$httpExt()
                .post('/api/constructionTask/queryTaskByRecord', {
                    current: current,
                    size: size
                })
                .then(
                    response => {
                        console.log('返回值', response);
                        let format = this.formatResult(response.data.records);
                        this.loading = false;
                        this.total = response.data.total;
                        this.tableData = format;
                    },
                    response => {
                        Notification.error({
                            title: '异常',
                            message: response.msg
                        });
                    }
                );
        },
        // 查询
        async resultQuery() {
            this.loading = true;
            this.$httpExt()
                .post('/api/constructionTask/queryTaskByRecord', {
                    workOrderNumber: this.workOrderNo,
                    taskStatus: this.workOrderStatus,
                    taskName: this.taskName,
                    projectName: this.projectName,
                    current: 1,
                    size: 10
                })
                .then(
                    response => {
                        console.log('返回值', response);
                        let format = this.formatResult(response.data.records);
                        this.loading = false;
                        this.total = response.data.total;
                        this.tableData = format;
                    },
                    response => {
                        Notification.error({
                            title: '异常',
                            message: response.msg
                        });
                    }
                );
        },
        // 重置搜索条件
        resetQuery() {
            this.workOrderStatus = '';
            this.workOrderNo = '';
            this.taskName = '';
            this.orderTableData(1, this.pageSize);
        },
        // 选中行
        handleSelectionChange(level) {
            this.multipleSelection = level;
            if (!this.multipleSelection.length) return false;
            this.taskNameFlag = this.multipleSelection[0].taskName;
        },
        expandCondition() {
            this.expandConditionFlag = !this.expandConditionFlag;
        },
        // 任务处理
        sceneProspect() {
            if (this.multipleSelection.length !== 1) {
                this.$message({
                    type: 'warning',
                    message: '请选择一条数据'
                });
                return false;
            }
            this.taskId = null;
            this.taskNameTitle = null;
            this.workOrderNumber = null;
            this.taskId = this.multipleSelection[0].id;
            this.taskNameTitle = this.multipleSelection[0].taskName;
            this.workOrderNumber = this.multipleSelection[0].workOrderNumber;
            if (this.taskNameFlag === '1') {
                // 进场验收
                this.dialogFlag = true;
                this.title = '进场验收';
            } else if (this.taskNameFlag === '2') {
                // 隐蔽工程验收
                this.dialogFlag = true;
                this.title = '隐蔽工程验收';
                // this.hideFlag = true
            } else if (this.taskNameFlag === '3') {
                console.log('// 施工现场巡检');
                // 施工现场巡检
                this.sceneFlag = true;
            } else if (this.taskNameFlag === '4') {
                console.log('// 安全施工巡查');
                // 安全施工巡查
                this.safeFlag = true;
            }
        },
        // 关闭
        closeDialog() {
            this.sceneFlag = false;
            this.safeFlag = false;
            this.dialogFlag = false;
            // 刷新列表数据
            this.resultQuery();
        },
        //转义装修类型
        formatterOrderType(row) {
            let label;
            this.orderTypeList.forEach(element => {
                if (element.value === row.orderType) {
                    label = element.label;
                }
            });

            return label;
        }
    }
};
